package com.jiuyin.dianjing.ui.fragment.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.helper.ApiHelper;
import com.jiuyin.dianjing.api.interfaces.ProgressDialogCallback;
import com.jiuyin.dianjing.permission.XPermission;
import com.jiuyin.dianjing.ui.activity.container.ContainerActivity;
import com.jiuyin.dianjing.util.IMEUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Description:
 * File: BaseFragment.java
 * Create: 2019/04/29 15:13
 *
 * @Author: Administrator
 */
public abstract class BaseFragment extends Fragment {
    private Unbinder mUnBinder;
    private ProgressDialog mDialog;
    private ImageView btRight;
    private ImageView btLeft;
    protected ProgressDialogCallback progress = new ProgressDialogCallback() {
        @Override
        public void showProgressDialog() {
            showLoading();
        }

        @Override
        public void dismissProgressDialog() {
            dismissLoading();
        }
    };
    private CompositeDisposable mCompositeDisposable;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(setLayoutId(), container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    public abstract void fetchData();

    protected abstract int setLayoutId();

    protected abstract void initData();

    protected abstract void initView();

    /**
     * 初始化其它的
     */
    protected void initOthers() {

    }

    @Override
    public void onDestroyView() {
        mUnBinder.unbind();
        dispose();
        super.onDestroyView();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        initData();
        initView();
        initOthers();
        fetchData();
    }

    protected ApiHelper getApiHelper() {
        return ApiHelper.getInstance(getContext());
    }

    protected void goTargetActivity(Class<?> cls) {
        goTargetActivity(cls, null);
    }

    protected void goTargetActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getContext(), cls);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        if (getActivity() != null) {
            IMEUtil.hideInputMethod(getActivity());
        }
        startActivity(intent);
    }

    public void showLoading() {
        if (mDialog != null && mDialog.isShowing()) return;
        mDialog = new ProgressDialog(getContext());
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mDialog.setMessage(getString(R.string.progress_tips_def));
        mDialog.show();
    }

    protected void setRightButton(Activity activity, @DrawableRes int resourceId, View.OnClickListener onClickListener) {
        if (btRight == null) {
            btRight = activity.findViewById(R.id.iv_rightMenu);
        }
        btRight.setImageResource(resourceId);
        btRight.setVisibility(View.VISIBLE);
        if (onClickListener != null) {
            btRight.setOnClickListener(onClickListener);
        }
    }

    protected void setLeftButton(Activity activity, @DrawableRes int resourceId, View.OnClickListener onClickListener) {
        if (btLeft == null) {
            btLeft = activity.findViewById(R.id.iv_back);
        }
        btLeft.setImageResource(resourceId);
        btLeft.setVisibility(View.VISIBLE);
        if (onClickListener != null) {
            btLeft.setOnClickListener(onClickListener);
        }
    }

    /**
     * 显示返回键
     */
    protected void showReturnButton() {
        View back = getActivity().findViewById(R.id.iv_back);
        if (null != back) {
            back.setVisibility(View.VISIBLE);
            back.setOnClickListener(v -> getActivity().finish());
        }
    }

    public void dismissLoading() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    public void addDisposable(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    public void dispose() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void onDestroy() {
        dispose();
        super.onDestroy();
    }

    /**
     * Android M 全局权限申请回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
            grantResults) {
        XPermission.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     */
    public void startContainerActivity(String canonicalName) {
        startContainerActivity(canonicalName, null);
    }

    /**
     * 跳转容器页面
     *
     * @param canonicalName 规范名 : Fragment.class.getCanonicalName()
     * @param bundle        跳转所携带的信息
     */
    public void startContainerActivity(String canonicalName, Bundle bundle) {
        Intent intent = new Intent(getContext(), ContainerActivity.class);
        intent.putExtra(ContainerActivity.FRAGMENT, canonicalName);
        if (bundle != null) {
            intent.putExtra(ContainerActivity.BUNDLE, bundle);
        }
        startActivity(intent);
    }
}
